package com.origin.oss.configuration;

import com.origin.oss.exception.OssException;
import com.origin.oss.specification.IOssClient;
import com.origin.oss.specification.OssClientAdapter;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author yezh
 * @date 2024/2/22 17:23
 */
@Configuration(proxyBeanMethods = true)
@ComponentScan("com.origin.oss")
@MapperScan("com.origin.oss")
@Slf4j
public class OssAutoConfiguration implements InitializingBean {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public OssClientAdapter originOssClient() {
        Map<String, IOssClient> beans;
        try {
            beans = applicationContext.getBeansOfType(IOssClient.class);
        } catch (BeansException e) {
            log.error("获取 ossClient 失败", e);
            throw new OssException("获取 ossClient 失败");
        }
        return new OssClientAdapter(beans.values().iterator().next());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("OssAutoConfiguration loading...");
    }
}
